﻿@inherits BaseComponent

<Tabs Animated Class="@Model.Class" @bind-ActiveKey="@current">
    <TabBarExtraContentLeft>
        @Model.Left
    </TabBarExtraContentLeft>
    <ChildContent>
        @if (Model != null && Model.HasItem)
        {
            foreach (var item in Model.Items)
            {
                if (!item.IsVisible)
                    continue;

                if (item.Content != null)
                {
                    <TabPane Key="@item.Id" Tab="@Language?.GetTitle(item.Title)">
                        @item.Content
                    </TabPane>
                }
                else
                {
                    <TabPane Key="@item.Id" Tab="@Language?.GetTitle(item.Title)" />
                }
            }
        }
    </ChildContent>
    <TabBarExtraContentRight>
        @Model.Right
    </TabBarExtraContentRight>
</Tabs>

@code {
    private string current;

    [Parameter] public TabModel Model { get; set; }
    [Parameter] public Action<string> OnChange { get; set; }

    protected override void OnInitialized()
    {
        base.OnInitialized();
        Model.OnStateChanged = StateChanged;
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);
        if (current != Model.Current)
        {
            Model.Current = current;
            OnChange?.Invoke(current);
            Model.Change();
            await JSRuntime.FillTableHeightAsync();
        }
    }
}